﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using EntLibEC.Dac;
using EntLibEC.Cmn;
using EntLibEC.Info.Basic;
using System.Data.SqlClient;

namespace EntLibEC.Biz.Basic
{
    public class CustomizedCategoryManager
    {
        private CustomizedCategoryManager()
        {

        }

        private static CustomizedCategoryManager _instance;
        public static CustomizedCategoryManager GetInstance()
        {
            if (_instance == null)
            {
                _instance = new CustomizedCategoryManager();
            }
            return _instance;
        }

        public List<CustomizedCategoryInfo> GetCategoryList(int categoryType)
        {
        	string sql = " select * from Ent_Category_Customized with (nolock) where CategoryType=" + categoryType.ToString() + " order by sysno ";
			DataSet ds = SqlHelper.ExecuteDataSet(sql);
			if ( !Util.HasMoreRow(ds))
				return null;

            List<CustomizedCategoryInfo> categoryList = new List<CustomizedCategoryInfo>();
            
			foreach( DataRow dr in ds.Tables[0].Rows )
			{
				CustomizedCategoryInfo item = new CustomizedCategoryInfo();
				Map(item, dr);
				categoryList.Add(item);
			}

            return categoryList;
        }

        public bool CheckExist(int categorySysNo, int categoryType, int parentSysNo)
        {
            string strSql = "select * from Ent_Category_Customized with (nolock) where OriginCategorySysNo = " + categorySysNo.ToString()
                + " and categoryType=" + categoryType.ToString()
                + " and parentSysNo=" + parentSysNo.ToString();
            DataSet ds = SqlHelper.ExecuteDataSet(strSql);

            if (Util.HasMoreRow(ds))
                return true;
            else
                return false;
        }

        public void Insert(int categorySysNo, int categoryType, int parentSysNo, int userSysNo)
        {
            string strInsert = " Insert Ent_Category_Customized (CategoryType, ParentSysNo, OriginCategorySysNo, Status, CategoryName, URL, PromotionStatus, Priority, Note, LastEditUserSysNo, LastEditDate, IsAutoExpand) "
                + " values (@CategoryType, @ParentSysNo, @OriginCategorySysNo, @Status, @CategoryName, @URL, 0, 0, '', @LastEditUserSysNo, getdate(), 0)";

            SqlCommand cmd = new SqlCommand(strInsert);

            SqlParameter paramCategoryType = new SqlParameter("@CategoryType", SqlDbType.Int, 4);
            SqlParameter paramParentSysNo = new SqlParameter("@ParentSysNo", SqlDbType.Int, 4);
            SqlParameter paramOriginCategorySysNo = new SqlParameter("@OriginCategorySysNo", SqlDbType.Int, 4);
            SqlParameter paramStatus = new SqlParameter("@Status", SqlDbType.Int, 4);
            SqlParameter paramCategoryName = new SqlParameter("@CategoryName", SqlDbType.NVarChar, 50);
            SqlParameter paramURL = new SqlParameter("@URL", SqlDbType.NVarChar, 200);
            SqlParameter paramLastEditUserSysNo = new SqlParameter("@LastEditUserSysNo", SqlDbType.Int, 4);
            paramCategoryType.Value = categoryType;
            paramParentSysNo.Value = parentSysNo;
            paramOriginCategorySysNo.Value = categorySysNo;
            paramLastEditUserSysNo.Value = userSysNo;

            if (categoryType == 1)
            {
                Category1Info c1Info = (Category1Info)CategoryManager.GetInstance().GetC1Hash()[categorySysNo];

                paramStatus.Value = c1Info.Status;
                paramCategoryName.Value = c1Info.C1Name;
                paramURL.Value = "/site/index.aspx?cid=" + c1Info.C1ID;
            }
            else if (categoryType == 2)
            {
                Category2Info c2Info = (Category2Info)CategoryManager.GetInstance().GetC2Hash()[categorySysNo];
                paramStatus.Value = c2Info.Status;
                paramCategoryName.Value = c2Info.C2Name;
                paramURL.Value = string.Empty;

            }
            else if (categoryType == 3)
            {
                Category3Info c3Info = (Category3Info)CategoryManager.GetInstance().GetC3Hash()[categorySysNo];
                paramStatus.Value = c3Info.Status;
                paramCategoryName.Value = c3Info.C3Name;
                paramURL.Value = "/site/productlistbycategory.aspx?ctid=" + c3Info.SysNo;
            }

            cmd.Parameters.Add(paramCategoryType);
            cmd.Parameters.Add(paramParentSysNo);
            cmd.Parameters.Add(paramOriginCategorySysNo);
            cmd.Parameters.Add(paramStatus);
            cmd.Parameters.Add(paramCategoryName);
            cmd.Parameters.Add(paramURL);
            cmd.Parameters.Add(paramLastEditUserSysNo);

            SqlHelper.ExecuteNonQuery(cmd);

        
        }

        /// <summary>
        ///  初始化前台产品目录结构
        /// </summary>
        /// <param name="userSysNo"></param>
        public void InitializeCustomizedCategory(int userSysNo)
        {
            string strSP = "usp_CustomizedCategory_Initialize";

            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = strSP;
            cmd.CommandType = CommandType.StoredProcedure;

            SqlParameter parameter = new SqlParameter();
            parameter.ParameterName = "@LastEditUserSysNo";
            parameter.SqlDbType = SqlDbType.Int;
            parameter.Value = userSysNo;

            cmd.Parameters.Add(parameter);

            SqlHelper.ExecuteNonQuery(cmd);
        }

        private void Map(CustomizedCategoryInfo item, DataRow dr)
        {
            item.SysNo = Util.TrimIntNull(dr["SysNo"]);
            item.CategoryType = Util.TrimIntNull(dr["CategoryType"]);
            item.ParentSysNo = Util.TrimIntNull(dr["ParentSysNo"]);
            item.OriginCategorySysNo = Util.TrimIntNull(dr["OriginCategorySysNo"]);
            item.Status = Util.TrimIntNull(dr["Status"]);
            item.CategoryName = Util.TrimNull(dr["CategoryName"]);
            item.URL = Util.TrimNull(dr["url"]);
        }
    }
}
